import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class ListTable extends StatelessWidget {
  const ListTable({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('嵌套表单')),
        body: MyNestedTable(),
      ),
    );
  }
}

class MyNestedTable extends StatefulWidget {
  @override
  _MyNestedTableState createState() => _MyNestedTableState();
}

class _MyNestedTableState extends State<MyNestedTable> {
  List<Item> _data = generateItems(5);

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        return ExpansionPanelList(
          expansionCallback: (int panelIndex, bool isExpanded) {
            setState(() {
              _data[panelIndex].isExpanded = !isExpanded;
            });
          },
          children: [
            ExpansionPanel(
              headerBuilder: (BuildContext context, bool isExpanded) {
                return ListTile(
                  title: Text(_data[index].headerValue),
                );
              },
              body: Table(
                border: TableBorder.all(),
                children: _data[index].tableData.map<TableRow>((rowData) {
                  return TableRow(
                    children: rowData.map<Widget>((cellData) {
                      return TableCell(
                        child: Padding(
                          padding: EdgeInsets.all(8.0),
                          child: Text(cellData),
                        ),
                      );
                    }).toList(),
                  );
                }).toList(),
              ),
              isExpanded: _data[index].isExpanded,
            ),
          ],
        );
      },
      itemCount: _data.length,
    );
  }
  Widget buildRow1(Item){
    return Row(
      children: [
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('Header'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('ID'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('状态'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('创建时间'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('创建者'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('操作'),
        ),
      ],
    );
  }

  Widget buildHeader(){
    return Row(
      children: [
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('Header'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('ID'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('状态'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('创建时间'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('创建者'),
        ),
        Container(
          width: 50,
          height: 50,
          color: Colors.blue,
          child: Text('操作'),
        ),
      ],
    );
  }
}

class Item {
  Item({
    required this.headerValue,
    required this.tableData,
    this.isExpanded = false,
  });

  String headerValue;
  List<List<String>> tableData;
  bool isExpanded;
}

List<Item> generateItems(int numberOfItems) {
  return List<Item>.generate(numberOfItems, (int index) {
    return Item(
      headerValue: 'Item $index',
      tableData: [
        ['A1', 'B1', 'C1'],
        ['A2', 'B2', 'C2'],
        ['A3', 'B3', 'C3'],
      ],
    );
  });
}
